const fs = require("fs");
const Koa = require("koa");
const path = require("path");
const staticRouter = require("koa-static");
const koaBody = require("koa-body");
const Router = require("koa-router");

const app = new Koa();
const router = new Router();
const jsonPath = path.join(__dirname, "goodsList.json");

app.use(koaBody());
app.use(staticRouter(path.join(__dirname, "../dist")));

router.get("/api/goodsList", async ctx => {
  ctx.body = fs.readFileSync(jsonPath, { encoding: "utf8" });
});

router.post("/api/postGoods", async ctx => {
  const data = ctx.request.body;
  ctx.body = JSON.stringify({
    status: "ok",
    code: 1,
    data: data
  });
});

app.use(router.routes());
app.use(router.allowedMethods());

app.listen(9000, () => {
  console.log("Server is running: http://localhost:9000");
});
